<template>
  <el-container>
    <el-header>
      <el-tabs v-model="activeTab" @tab-click="handleTabClick">
        <el-tab-pane label="普通用户" name="common" />
        <el-tab-pane label="交管部门" name="traffic" />
        <el-tab-pane label="管理员" name="admin" />
      </el-tabs>
    </el-header>
    <el-main>
      <el-form :model="formData">
        <el-form-item label="用户名">
          <el-input v-model="formData.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密&nbsp;&nbsp;&nbsp;码 ">
          <el-input type="password" v-model="formData.password" placeholder="请输入密码" />
        </el-form-item>
        <el-button type="primary" @click="handleLogin">登录</el-button>
        <div style="display: flex; justify-content: space-between; width: 100%" v-if="isShow">
          <el-link @click="handleRegister">立即注册</el-link>
          <el-link @click="handleRetrieve">忘记密码</el-link>
        </div>
      </el-form>
    </el-main>
  </el-container>
</template>

<script setup>
import { ref,onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login } from '@/request/axios'
// 使用 useRouter 获取路由实例
const $router = useRouter()

// 定义响应式数据
const activeTab = ref('common')
const formData = ref({
  username: '',
  password: '',
  type: 'common'
})
let isShow = ref(true)
// 处理标签点击事件
const handleTabClick = (tab) => {
  formData.value.type = tab.props.name
  if (tab.props.name !== 'common') {
    isShow.value = false
  } else {
    isShow.value = true
  }
}

// 处理登录逻辑
const handleLogin = async () => {
  // 将账号密码存储到本地
  localStorage.setItem('account', JSON.stringify(formData.value))
  try {
    // 发送登录请求
    const {username,password,type} = formData.value
    console.log(username,password,type);
    const response = await login(username,password,type)
    // 登录成功则按照对应type 去不同页面
    if (response.data.code === 200) {
      if (formData.value.type === 'common') {
        $router.push('/common')
      } else if (formData.value.type === 'traffic') {
        $router.push('/traffic')
      } else if (formData.value.type === 'admin') {
        $router.push('/admin')
      }
      // 将token存储到本地
      localStorage.setItem('token', response.data.token)
    } else  {
        ElMessage.error(`${response.data.msg}`)
      } 
  } catch (error) {
    ElMessage.error('登录时出现错误')
  }
}

// 处理注册逻辑
const handleRegister = () => {
  $router.push({ path: '/register' })
}
// 处理修改密码逻辑
const handleRetrieve = () => {
  // 切换到注册表单
  // 实现逻辑可以根据你的需求进行调整
}
onMounted(()=>{
  // 自动填充密码
  const localInfo  = JSON.parse(localStorage.getItem("account"));
if(localInfo){
  if(localInfo.username && localInfo.password){
    formData.value.username = localInfo.username
    formData.value.password = localInfo.password
  }
}
})
</script>

<style scoped>
.el-container {
  width: 400px;
  margin: 0 auto;
  padding-top: 50px;
}

.el-main {
  background-color: white;
  padding: 20px;
  border-radius: 8px;
}

.el-button {
  width: 100%;
  margin-top: 20px;
}
</style>
